package com.example.shixun.controller;

import com.example.shixun.pojo.*;
import com.example.shixun.service.PostRlyService;
import com.example.shixun.service.PostService;
import com.example.shixun.service.UserService;
import com.example.shixun.utils.GetIdByToken;
import com.example.shixun.utils.GetNowTime;
import com.example.shixun.utils.PageInfoUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostService postService;
    @Autowired
    GetIdByToken getIdByToken;
    @Autowired
    PostRlyService postRlyService;
    @Autowired
    UserService userService;
    @ApiOperation("贴吧列表")
    @GetMapping("/list")
    public Result postList(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "orderBy", defaultValue = "create_time") String orderBy,
                           @RequestParam(value = "order", defaultValue = "desc") String order,
                           Post post){
        System.out.println(post.toString());
        PageInfoUtil<Post> pageInfoUtil=new PageInfoUtil<Post>();
        String[] orderByArr = {"likes","title","create_time","replies"};
        String orderByStr = "";
        System.out.println("orderBy:"+orderBy+" order:"+order);
        if (orderBy!=null&&orderBy!="" && Arrays.asList(orderByArr).contains(orderBy.toLowerCase())) {
            orderByStr = String.format("%s %s", orderBy.toLowerCase(), "asc".equalsIgnoreCase(order) ? "asc" : "desc");
        } else {
            // 默认排序
            orderByStr = "create_time desc";
        }
        pageInfoUtil.setPageNum(pageNum);
        pageInfoUtil.setPageSize(pageSize);
        pageInfoUtil.setParams(post);
        pageInfoUtil.setOrderBy(orderByStr);
        PageInfoUtil<Post> pageInfo = postService.findAll(pageInfoUtil);
//        System.out.println(pageInfo.toString());
        return Result.success(pageInfo);
    }
    @ApiOperation("发布贴吧")
    @PostMapping("/publish")
    public Result publishPost(@RequestHeader("token") String token,
                              @RequestBody Post post){
        System.out.println(post.toString());
        if(post.getTitle()==null||post.getTitle()==""||post.getContent()==null||post.getContent()=="") return Result.fail("请填写完整信息！");
        if(post.getCreateTime()==null) post.setCreateTime(GetNowTime.getNowTime());
        post.setUserId(getIdByToken.getIdByToken(token));
        post.setUserName(getIdByToken.getNameByToken(token));
        int n = postService.savePost(post);
        if(n>0) return Result.success("发布成功！",n);
        return Result.fail("发布失败");
    }
    @ApiOperation("根据id查找帖子")
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")Integer id){
        return Result.success(postService.selectPostById(id));
    }
    @ApiOperation("根据postId查找回帖")
    @GetMapping("/findRlyByPostId")
    public Result findRlyById(@RequestParam("id")Integer id){
        List<PostReply> postReplyList=postRlyService.findByPostId(id);
        List<PostReplyDTO> resLists=new ArrayList<>();
        for(PostReply postReply:postReplyList){
            PostReplyDTO dto=new PostReplyDTO();
            dto.setId(postReply.getId());
            dto.setUserId(postReply.getUserId());
            dto.setPostId(postReply.getPostId());
            dto.setContent(postReply.getContent());
            User user= (User) userService.getUserById(dto.getUserId()).getData();
            if(user!=null){
                dto.setUserName(user.getName());
                dto.setUserAvatar(user.getAvatar());
                resLists.add(dto);
            }
        }
        return Result.success(resLists);
    }
    @ApiOperation("根据用户id查找帖子")
    @GetMapping("/findMyPost")
    public Result findMyPost(@RequestHeader("token") String token,
                             @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                             @RequestParam(value = "orderBy", defaultValue = "create_time") String orderBy,
                             @RequestParam(value = "order", defaultValue = "desc") String order,
                             Post post){
        Integer uid=getIdByToken.getIdByToken(token);
        post.setUserId(uid);
        PageInfoUtil<Post> pageInfoUtil=new PageInfoUtil<Post>();
        String[] orderByArr = {"likes","title","create_time","replies"};
        String orderByStr = "";
        System.out.println("orderBy:"+orderBy+" order:"+order);
        if (orderBy!=null&&orderBy!="" && Arrays.asList(orderByArr).contains(orderBy.toLowerCase())) {
            orderByStr = String.format("%s %s", orderBy.toLowerCase(), "asc".equalsIgnoreCase(order) ? "asc" : "desc");
        } else {
            // 默认排序
            orderByStr = "create_time desc";
        }
        pageInfoUtil.setPageNum(pageNum);
        pageInfoUtil.setPageSize(pageSize);
        pageInfoUtil.setParams(post);
        pageInfoUtil.setOrderBy(orderByStr);
        PageInfoUtil<Post> pageInfo = postService.findAll(pageInfoUtil);
        return Result.success(pageInfo);
    }
    @ApiOperation("根据id删除贴")
    @GetMapping("/delete")
    public Result deletePost(@RequestParam("id") Integer id){
        return Result.success(postService.deletePostById(id));
    }
}
